/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChangeAnnotator.java
 *
 * Created on Oct 1, 2010, 8:13:13 PM
 */

package ResultEditor.Annotator;

import java.awt.Dimension;

/**
 *
 * @author Chris
 */
public class ChangeAnnotator extends javax.swing.JFrame {
    // variables
    private UserInterface.GUI FATHER_WINDOW_HANDLE;
    private String oldannotator_username, oldannotator_userid;

    /** Creates new form ChangeAnnotator */
    public ChangeAnnotator(UserInterface.GUI _FATHER_WINDOW_HANDLE) {
        FATHER_WINDOW_HANDLE = _FATHER_WINDOW_HANDLE;

        // init and set dialog location
        initComponents();
        setLocation();

        // show current annotator's name
        updateScreen_for_CurrentAnnoator();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jCheckBox_apply_toAll = new javax.swing.JCheckBox();
        jCheckBox_apply_toRelated = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
                formWindowLostFocus(evt);
            }
        });

        jLabel1.setText("Current Annotator Name:");

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel2.setText("Current Annotator ID:");

        jCheckBox_apply_toAll.setText("Apply this User to all existing annotations.");
        jCheckBox_apply_toAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_apply_toAllActionPerformed(evt);
            }
        });

        jCheckBox_apply_toRelated.setSelected(true);
        jCheckBox_apply_toRelated.setText("Apply changes to related annotations.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox_apply_toRelated)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))
                    .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addComponent(jCheckBox_apply_toAll))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(120, 120, 120)
                .addComponent(jCheckBox_apply_toRelated)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox_apply_toAll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        exitProc();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowLostFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowLostFocus
        exitProc();
    }//GEN-LAST:event_formWindowLostFocus

    private void jCheckBox_apply_toAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_apply_toAllActionPerformed
        jCheckBox_apply_toRelated.setEnabled(!jCheckBox_apply_toAll.isSelected());
    }//GEN-LAST:event_jCheckBox_apply_toAllActionPerformed

    /**exitProc process*/
    private void exitProc(){
        try{
            String text = this.jTextField1.getText();
            String id = this.jTextField2.getText();
            if (( text == null )||(id==null)){
                commons.Tools.beep();
                return;
            }else if (( text.trim().length() < 1)||( id.trim().length() < 1)) {
                commons.Tools.beep();
                return;
            }else{
                ResultEditor.Annotator.Manager.setCurrentAnnotator( text.trim() );
                ResultEditor.Annotator.Manager.setCurrentAnnotatorID( id.trim() );
                FATHER_WINDOW_HANDLE.updateScreen_for_variables();
                FATHER_WINDOW_HANDLE.removeSelection();
            }

            applyChanges(text, id, this.oldannotator_username, this.oldannotator_userid);

        }catch(Exception ex){
        
        }


        this.setVisible(false);
        FATHER_WINDOW_HANDLE.removeSelection();
        this.dispose();
    }
    

    /**
     * By users' opinions got from checkboxes, apply change of current annotator
     * informations to existed annotations.
     */
    private void applyChanges(final String annotatorname_new, final String annotatorid_new,
            final String oldannotatorname, final String oldannotatorid){
        if(this.jCheckBox_apply_toAll.isSelected()){
            new Thread(){
                @Override
                public void run(){
                    ResultEditor.Annotations.Depot annotationdepot = new
                            ResultEditor.Annotations.Depot();
                    annotationdepot.changeAnnotator_toAll(annotatorname_new, annotatorid_new);
                }}.start();
            return;
        }
        if(this.jCheckBox_apply_toRelated.isSelected()){
            new Thread(){
                @Override
                public void run(){
                    ResultEditor.Annotations.Depot annotationdepot = new
                            ResultEditor.Annotations.Depot();
                    annotationdepot.changeAnnotator_toRelated(annotatorname_new, annotatorid_new, oldannotatorname, oldannotatorid);
                }}.start();
        }
    }

    /**Show current annotator's name on this dialog*/
    private void updateScreen_for_CurrentAnnoator(){
        try{
            String currentannotator = ResultEditor.Annotator.Manager.getCurrentAnnotator();
            this.jTextField1.setText(currentannotator);
            oldannotator_username = currentannotator;
            String currentannotatorid = ResultEditor.Annotator.Manager.getCurrentAnnotatorID();
            this.jTextField2.setText(currentannotatorid);
            oldannotator_userid = currentannotatorid;
        }catch(Exception ex){

        }
    }

    /**Set the location at the middle of its faterwindow.*/
    private void setLocation(){

        // set frame size of this dialog
        this.setPreferredSize(new Dimension(618,380));

        // valid check
        if( FATHER_WINDOW_HANDLE == null )
            return;

        // calculate the location to set this dialog at the middle of parent
        // dialog
        int parentX = FATHER_WINDOW_HANDLE.getX(),
            parentY = FATHER_WINDOW_HANDLE.getY();
        int parentWidth = FATHER_WINDOW_HANDLE.getWidth(),
            parentHeight = FATHER_WINDOW_HANDLE.getHeight();

        int width = this.getWidth(), height = this.getHeight();
        int x = parentX + (int)(parentWidth  -  width)/2,
            y = parentY + (int)(parentHeight - height)/2;

        // set location of this dialog
        this.setLocation(x, y);
    }

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChangeAnnotator().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBox_apply_toAll;
    private javax.swing.JCheckBox jCheckBox_apply_toRelated;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
